package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.ConsultationApplyBo;
import com.hospital.workstation.domain.vo.ConsultationApplyVo;
import com.hospital.workstation.domain.ConsultationApply;
import com.hospital.workstation.mapper.ConsultationApplyMapper;
import com.hospital.workstation.service.IConsultationApplyService;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会诊申请Service业务层处理
 *
 * @author whb
 * @date 2023-02-27
 */
@RequiredArgsConstructor
@Service
public class ConsultationApplyServiceImpl implements IConsultationApplyService {

    private final ConsultationApplyMapper baseMapper;

    /**
     * 查询会诊申请
     */
    @Override
    public ConsultationApplyVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询会诊申请列表
     */
    @Override
    public TableDataInfo<ConsultationApplyVo> queryPageList(ConsultationApplyBo bo, PageQuery pageQuery) {
        // 查询会诊申请列表
        Wrapper<ConsultationApply> qw = buildQueryWrapper2(bo);
        Page<ConsultationApplyVo> result = baseMapper.customPageList(pageQuery.build(), qw);
        result.getRecords().stream().forEach(consultationApplyVo -> {
            // 是否已处理
            consultationApplyVo.setIsDispose((StringUtils.isNotBlank(consultationApplyVo.getDisposeContent()
            ) && consultationApplyVo.getDisposeTime() != null) ? "是" : "否");
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询会诊申请列表
     */
    @Override
    public List<ConsultationApplyVo> queryList(ConsultationApplyBo bo) {
        LambdaQueryWrapper<ConsultationApply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ConsultationApply> buildQueryWrapper(ConsultationApplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ConsultationApply> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), ConsultationApply::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getConsultationContent()), ConsultationApply::getConsultationContent, bo.getConsultationContent());
        lqw.eq(bo.getReceiveManId() != null, ConsultationApply::getReceiveManId, bo.getReceiveManId());
        lqw.eq(bo.getReceiveTime() != null, ConsultationApply::getReceiveTime, bo.getReceiveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDisposeContent()), ConsultationApply::getDisposeContent, bo.getDisposeContent());
        lqw.eq(bo.getDisposeTime() != null, ConsultationApply::getDisposeTime, bo.getDisposeTime());
        return lqw;
    }

    private Wrapper<ConsultationApply> buildQueryWrapper2(ConsultationApplyBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<ConsultationApply> qw = Wrappers.query();
        qw.like(StringUtils.isNotBlank(bo.getUserName()), "hca.user_name", bo.getUserName())
            .eq(bo.getMedicalRecordNo() != null, "hca.medical_record_no", bo.getMedicalRecordNo())
            .between(params.get("beginDate") != null && params.get("endDate") != null,
                "hca.receive_time", params.get("beginDate") + " 00:00:00", params.get("endDate") + " 23:59:59");
        return qw;
    }

    /**
     * 新增会诊申请
     */
    @Override
    public Boolean insertByBo(ConsultationApplyBo bo) {
        ConsultationApply add = BeanUtil.toBean(bo, ConsultationApply.class);
        validEntityBeforeSave(add);
        // 判断是否处理
        if (StringUtils.isNotBlank(bo.getDisposeContent())) {
            add.setDisposeTime(LocalDateTime.now());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会诊申请
     */
    @Override
    public Boolean updateByBo(ConsultationApplyBo bo) {
        ConsultationApply update = BeanUtil.toBean(bo, ConsultationApply.class);
        validEntityBeforeSave(update);
        // 设置处理时间
        update.setDisposeTime(LocalDateTime.now());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ConsultationApply entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会诊申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
